package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import svc.FileSvc;
import svc.ResultSvc;
import constants.Constants;
import domain.file.FileDetails;
import domain.result.Result;
import functions.Functions;

/**
 * Servlet implementation class Scenario
 */
//@WebServlet("/Scenario")
public class Scenario extends HttpServlet {
	//Logger log = Logger.getLogger("MyLog");
	private static final long serialVersionUID = 1L;
	private FileSvc fileSvc = new FileSvc();
	private Functions funSvc = new Functions();
	private ResultSvc resSvc = new ResultSvc();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Scenario() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session = request.getSession(false);
		if (session == null) {
			// No Existing Session Found Please Login Again
		}
		
		String servletPath = request.getServletPath();
		System.out.println("Servlet Path: "+servletPath);
		
		int userId = (Integer) session.getAttribute(Constants.SESSION_USER_ID);
		
		if(servletPath.equals("/jsp/deleteScenario")){
			if(null != request.getParameter("deleteScenarioId")){
				int fileNo = Integer.parseInt(request.getParameter("deleteScenarioId"));
				System.out.println("Scenario Id: "+fileNo);
				
				fileSvc.deleteFile(fileNo);
				ArrayList<FileDetails> userFileList = fileSvc.getUserFileList(userId);
				ArrayList<FileDetails> allFileList = fileSvc.getAllFileList(userId);
				session.setAttribute(Constants.SESSION_ALL_SCENARIO_LIST, allFileList);
				session.setAttribute(Constants.SESSION_USER_SCENARIO_LIST, userFileList);
				
				request.getRequestDispatcher("/jsp/dataset.jsp").forward(request, response);
			}
		}else if(servletPath.equals("/jsp/createScenario")){
			if(null != request.getParameter("createScenarioId")){
				int defaultFileNo = Integer.parseInt(request.getParameter("createScenarioId"));
				System.out.println("Default Scenario Id: "+defaultFileNo);
				
				String fileName = request.getParameter("newFileName");
				String fileDesc = request.getParameter("newFileDescription");
				
				fileSvc.createNewFile(userId, fileName, fileDesc, defaultFileNo);
				ArrayList<FileDetails> userFileList = fileSvc.getUserFileList(userId);
				ArrayList<FileDetails> allFileList = fileSvc.getAllFileList(userId);
				session.setAttribute(Constants.SESSION_ALL_SCENARIO_LIST, allFileList);
				session.setAttribute(Constants.SESSION_USER_SCENARIO_LIST, userFileList);
				
				request.getRequestDispatcher("/jsp/dataset.jsp").forward(request, response);
			}
		}else if(servletPath.equals("/jsp/showResult")){
			if(null != request.getParameter("selectScenarioId1") && null != request.getParameter("selectScenarioId2")){
				int fileNoA = Integer.parseInt(request.getParameter("selectScenarioId1"));
				int fileNoB = Integer.parseInt(request.getParameter("selectScenarioId2"));
				
				System.out.println("Current Scenario Id 1: "+fileNoA);
				System.out.println("Current Scenario Id 2: "+fileNoB);
				
				ArrayList<FileDetails> allFileList = fileSvc.getAllFileList(userId);
				boolean analyzeFileA = true;
				boolean analyzeFileB = true;
				for(int i=0; i<allFileList.size(); i++){
					if(allFileList.get(i).getFileNo()==fileNoA && allFileList.get(i).getAnalyzed()==Constants.ANALYZED){
						analyzeFileA = false;
					}
					if(allFileList.get(i).getFileNo()==fileNoB && allFileList.get(i).getAnalyzed()==Constants.ANALYZED){
						analyzeFileB = false;
					}
				}
				if(analyzeFileA){
					funSvc.startSimulation(fileNoA);
					if(fileNoA==fileNoB){
						analyzeFileB = false;
					}
				}
				if(analyzeFileB){
					funSvc.startSimulation(fileNoB);	
				}
				
				Result resultA = resSvc.getResult(fileNoA);
				Result resultB = resSvc.getResult(fileNoB);
				
				request.setAttribute("resultA", resultA);
				request.setAttribute("resultB", resultB);
				
				request.getRequestDispatcher("/jsp/result.jsp").forward(request, response);
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
